package com.xoa.service.attend.wrapper;

import com.xoa.util.DateFormat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by gsb on 2018/9/28.
 */
public class AttendUtil {

    public static List<String> getMonthBetweenDateStr(String minDate, String maxDate,List<String> holidayList) throws ParseException {
        List<String> listDate = new ArrayList<>();
        listDate.add(minDate);
        Calendar startCalendar = Calendar.getInstance();
        Calendar endCalendar = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = df.parse(minDate);
        startCalendar.setTime(startDate);
        Date endDate = df.parse(maxDate);
        endCalendar.setTime(endDate);
        while(true){
            startCalendar.add(Calendar.DAY_OF_MONTH, 1);
            if(startCalendar.getTimeInMillis() < endCalendar.getTimeInMillis()){
                if(holidayList != null && holidayList.size() > 0) {
                    for (String date : holidayList) {
                        if (!isSameDate(DateFormat.DateToStr(date), startCalendar.getTime())) {
                            listDate.add(df.format(startCalendar.getTime()));
                        }
                    }
                }else {
                    listDate.add(df.format(startCalendar.getTime()));
                }
            }else{
                break;
            }
        }
        listDate.add(maxDate);
        removeDuplicate(listDate);
        return listDate;
    }


    /**
     * 判断两个日期是否为同一天
     * @author houwangxin
     */
    public static boolean isSameDate(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);

        boolean isSameYear = cal1.get(Calendar.YEAR) == cal2
                .get(Calendar.YEAR);
        boolean isSameMonth = isSameYear
                && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
        boolean isSameDate = isSameMonth
                && cal1.get(Calendar.DAY_OF_MONTH) == cal2
                .get(Calendar.DAY_OF_MONTH);

        return isSameDate;
    }

    /**
     * 循环list中的所有元素然后删除重复
     * @param list 待去重的list
     * @return 去重后的list
     */
    public static <T> List<T> removeDuplicate(List<T> list){
        for(int i=0;i<list.size()-1;i++){
            for(int j=list.size()-1;j>i;j--){
                if (list.get(j).equals(list.get(i))){
                    list.remove(j);
                }
            }
        }
        return list;
    }


    public static List<AttendWrappers> removeDuplicate3(List<AttendWrappers> list){
        for(int i=0;i<list.size()-1;i++){
            for(int j=list.size()-1;j>i;j--){
                if (list.get(j).getDateAll().equals(list.get(i).getDateAll())){
                    list.remove(j);
                }
            }
        }
        return list;
    }


    public static List<String> removeDuplicate2(List<String> list){
        for(String str : list){
            for(String str2 : list){
                if (str2.equals(str)){
                    list.remove(str);
                    list.remove(str2);
                }
            }
        }
        return list;
    }


    public static List<Date> removeDuplicate(List<String> list,List<Date> list2){
        for(Date date : list2){
            for(String str : list){
                if (DateFormat.getDatestr(date).equals(str)){
                    list2.remove(date);
                }
            }
        }
        return list2;
    }
}
